package com.tedu.egnotice1.controller;

import com.tedu.egnotice1.mapper.NoticeMapper;
import com.tedu.egnotice1.pojo.dto.NoticeAddParam;
import com.tedu.egnotice1.pojo.dto.NoticeAddQuery;
import com.tedu.egnotice1.pojo.dto.NoticeUpdateParam;
import com.tedu.egnotice1.pojo.entity.Notice;
import com.tedu.egnotice1.pojo.vo.NoticeDetailInfoVO;
import com.tedu.egnotice1.pojo.vo.NoticeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @PostMapping("/v1/notice/add")
    @ResponseBody
    public String insertNotice(NoticeAddParam noticeAddParam){
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(100l);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        int num = noticeMapper.insert(notice);
        return num>0?"添加成功":"添加失败";
    }
    @GetMapping("/v1/notice/list")
    @ResponseBody
    public List<NoticeListVO> select(NoticeAddQuery noticeQuery){
        List<NoticeListVO> list = noticeMapper.select(noticeQuery);
        return list;
    }
    @GetMapping("/v1/notice/detail")
    @ResponseBody
    public NoticeDetailInfoVO selectById(Long id){
        NoticeDetailInfoVO detailInfoVO = noticeMapper.selectById(id);
        return detailInfoVO;
    }
    @DeleteMapping("/v1/notice/del")
    @ResponseBody
    public String delete(Long id){
        int num = noticeMapper.delete(id);
        return num>0?"删除成功":"删除失败";
    }
    @PostMapping("/v1/notice/update")
    @ResponseBody
    public String update(NoticeUpdateParam noticeUpdateParam){
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(new Date());
        int num = noticeMapper.update(notice);
        return num>0?"修改成功":"修改失败";
    }
}
